// ----------------------------------------------------------------------------
// CheckTablespace.java 20101206 ndarkduck version 1.0
// e-mail ndarkduck[ta]gma1l.com, this is a code released under the GPL 3.0
// url: http://code.google.com/p/checkibm/
//
//
package com.google.code.checkibm;

public class Main
{
    private static void displayUsage()
    {
        System.out.println("Usage:");
        System.out.println("      $JAVAPATH/java -cp $PATH/db2jcc.jar:$checkibm.jar com.google.code.checkibm.Main [-vvv|--verbose] ");
        System.out.println("      [-H <host>] [-p <port>] [-w <float>] [-c <float>] -q check_item ");
        System.out.println("      [-F|--flags] [-l <userame>] [-a <password>]");
        System.out.println("Flag format:      ");
        System.out.println("            -F var1=value,var2=value     ");
        System.out.println("Avilable check_items:      ");
        System.out.println(" * db2_tablespace,db2hadr      ");
        
        
    }

    static int return_code = 0; // 'OK'=>0,'WARNING'=>1,'CRITICAL'=>2,'UNKNOWN'=>3,'DEPENDENT'=>4
    static float warn_at = 0;
    static float crit_at = 0;
    static int verbosity = 0;
    static String host = "localhost";
    static String port = "50000";
    static final String VERSION = "0.2.1";

    static String performance_data = "";
    static String message_data = "";

    public static void main(String args[])
    {
        CheckPlugin plugin = null;
        String check_item = "";
        String flags="";
        String username = "";
        String password = "";
        int timeout = 0;

        /*
         * * * -V version (--version)* -h help (--help) -t timeout (--timeout)
         * -w warning threshold (--warning) -c critical threshold (--critical)
         * -H hostname (--hostname)* -v verbose (--verbose) -C SNMP community
         * (--community) -a authentication password (--authentication) -l login
         * name (--logname) -p port or password (--port or
         * --passwd/--password)monitors operational -u url or username (--url or
         * --username)
         */
        try
        {
            // Pharse arguments
            for (int i = 0; i < args.length; i++)
            {
                if (args[i].charAt(0) == '-' && args[i].length() > 1)
                {
                    // Group of flags that expect no further argument
                    if (args[i].indexOf("-v") == 0)
                    {
                        for (int j = 0; j < args[i].length(); j++)
                            if (args[i].charAt(j) == 'v')
                                verbosity++;
                    } else if (args[i].equals("--help") || args[i].equals("-h"))
                    {
                        displayUsage();
                        System.exit(3);
                    } else if (args[i].equals("--version") || args[i].equals("-V"))
                    {
                        System.out.println("check_ibm " + VERSION);
                        System.exit(3);
                    } else if (args.length >= i + 1)
                    {
                        if (args[i].equals("-H")) // host
                            host = args[i + 1];
                        else if (args[i].equals("-p")) // port
                            port = args[i + 1];
                        else if (args[i].equals("-w")) // warning
                            warn_at = Float.parseFloat(args[i + 1]);
                        else if (args[i].equals("-c")) // critical
                            crit_at = Float.parseFloat(args[i + 1]);
                        else if (args[i].equals("-q")) // check item
                            check_item = args[i + 1];
                        else if (args[i].equals("-l")) // check item
                            username = args[i + 1];
                        else if (args[i].equals("-a")) // check item
                            password = args[i + 1];
                        else if (args[i].equals("-t")) // check item
                            timeout = Integer.parseInt(args[i + 1]);
                        else if (args[i].equals("-F") || args[i].equals("--flags")) // Pass flags for plugins
                        {
                            for(int j=1;i+j<args.length && args[i+j].indexOf("-") != 0; j++)
                                flags += args[i + j];
                            flags+=",timeout="+timeout;
                        }
                        i++;
                    }

                } else
                    throw new Exception("error parsing command line, please check your sintax");

            }            
            // End parsing arguments
            // Start checking, this code is small for starting
            if (check_item.equals("") )
                throw new UsageException();
            else if (check_item.equals("db2_tablespace"))            
                plugin = new com.google.code.checkibm.db2.CheckTablespace(host, port, flags, username, password, crit_at, warn_at);
            else if(check_item.equals("db2_hadr"))
                plugin = new com.google.code.checkibm.db2.CheckDb2HADR(host, port, flags, username, password, crit_at, warn_at);
            
            return_code = plugin.checkStatus();
            message_data = plugin.get_message();
            performance_data = plugin.get_performance_data();
            // Stop checking
            
        }catch (UsageException e)
        {
            displayUsage();
            System.out.print(e);
            
        }
        catch (Exception e)
        {
            return_code = 3;
            if (verbosity > 0)
                message_data = e.toString();
        }

        if (return_code == 0)
        {
            System.out.println("OK: " + message_data + " |" + performance_data);
            System.exit(0);
        } else if (return_code == 1)
        {
            System.out.println("WARN: " + message_data + " |" + performance_data);
            System.exit(1);
        } else if (return_code == 2)
        {
            System.out.println("CRIT: " + message_data + " |" + performance_data);
            System.exit(2);
        } else
        {
            System.out.println("UNK: " + message_data);
            System.exit(3);
        }

    }
}
